package com.cloudkinto.appletservice.stock.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.appletservice.stock.AppletStockRecordService;
import com.cloudkinto.appletservice.stock.vo.record.AppletStockRecordPageRes;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.extentity.StockRecordPageDto;
import com.cloudkinto.service.finance.CompanyAccountService;
import com.cloudkinto.service.finance.vo.IdAndNameEntity;
import com.cloudkinto.service.orderoutput.OrderOutputService;
import com.cloudkinto.service.region.JpRegionService;
import com.cloudkinto.service.shop.ShopService;
import com.cloudkinto.service.stock.vo.StockRecordDetailRes;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author 张永远
 * @since 2020-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class AppletStockRecordServiceImpl extends ServiceImpl<StockRecordDao, StockRecordDo> implements AppletStockRecordService {
    @Autowired
    private StockRecordDao dao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private StorageDao storageDao;



    @Override
    public StockRecordDetailRes detail(Long id) {
        StockRecordDo entityDo = dao.selectById(id);
        StockRecordDetailRes res = new StockRecordDetailRes();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private StockRecordDetailRes handlerDetailAfter(StockRecordDetailRes res) {
        return res;
    }


    @Override
    public PageResult getListPage(Map<String, String> map) {
        IPage<StockRecordPageDto> page = pageInit(map);
        List<StockRecordPageDto> pageDtoList = dao.listPage(page, map);
        List<AppletStockRecordPageRes> resList = handlerListAfter(pageDtoList);
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }



    //查询分页列表之后对列表数据的处理
    private List<AppletStockRecordPageRes> handlerListAfter(List<StockRecordPageDto> list) {
        List<AppletStockRecordPageRes> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (StockRecordPageDto pageDto : list) {
                AppletStockRecordPageRes res = new AppletStockRecordPageRes();
                res.setId(pageDto.getId());
                res.setCanotSellCount(pageDto.getCanotSellCount());
//                res.setProductInfoId(pageDto.getProductInfoId());
                res.setCanSellCount(pageDto.getCanSellCount());
                res.setCommoditySku(pageDto.getCommoditySku());
                res.setStorageSku(pageDto.getStorageSku());
                res.setProductName(pageDto.getProductName());
                res.setNoDelivered(pageDto.getNoDelivered());
                res.setInTransit(pageDto.getInTransit());
                res.setUpdateTime(pageDto.getUpdateTime());
                ShopDo shopDo = shopDao.selectById(pageDto.getShopId());
                if (shopDo != null) {
                    res.setShop(shopDo.getShopName());
                }
                StorageDo storageDo = storageDao.selectById(pageDto.getStorageId());
                if (storageDo != null) {
                    res.setStorage(storageDo.getStorageName());
                }
                result.add(res);
            }
        }
        return result;
    }

    private IPage<StockRecordPageDto> pageInit(Map<String, String> map) {
        IPage<StockRecordPageDto> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }
}
